package vn.axonactive.vision.dao.impl;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.process.data.persistence.IIvyEntityManager;

import vn.axonactive.vision.dao.EmailDao;
import vn.axonactive.vision.entity.Email;

@SuppressWarnings("unchecked")
public class EmailDaoImpl implements EmailDao {
	
	IIvyEntityManager iem = Ivy.persistence().get("WebMailDBPersistence");

	@Override
	public List<Email> findAllEmailsBelongToUser(String email) {
		List<Email> emails = (List<Email>) iem
				.createQuery("select e from Email e where e.receiver = :receiver and e.is_draft = :isDraft order by e.send_date desc")
				.setParameter("receiver", email)
				.setParameter("isDraft", false)
				.getResultList();
		return emails;
	}

	@Override
	public List<Email> findDraftEmailsBelongToUser(String email) {
		
		List<Email> emails = (List<Email>) iem
				.createQuery("select e from Email e where e.sender = :sender and e.is_draft = :isDraft order by e.send_date desc")
				.setParameter("sender", email)
				.setParameter("isDraft", true)
				.getResultList();
		return emails;
	}
	
	

}
